//
//  OrgReportViewController.m
//  EPBPro
//
//  Created by wucha on 2018/8/13.
//  Copyright © 2018年 fc. All rights reserved.
//

#import "OrgReportViewController.h"
#import "HttpReportAction.h"
#import "PNLineChart.h"
#import "PNLineChartData.h"
#import "PNLineChartDataItem.h"
#import "OrgMainReportInfo.h"
#import "OrgReportInfo.h"

@interface OrgReportViewController ()<PNChartDelegate>
@property (nonatomic,strong) PNLineChart *lineChart;

@end

@implementation OrgReportViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.titleName = @"组织机构收益报表";
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.lineChart.backgroundColor = [UIColor whiteColor];
    self.lineChart.yGridLinesColor = [UIColor grayColor];
    [self.lineChart.chartData enumerateObjectsUsingBlock:^(PNLineChartData *obj, NSUInteger idx, BOOL *stop) {
        obj.pointLabelColor = [UIColor blackColor];
    }];
    
    NSNumberFormatter *barChartFormatter = [[NSNumberFormatter alloc] init];
    barChartFormatter.numberStyle = NSNumberFormatterCurrencyStyle;
    
    self.lineChart = [[PNLineChart alloc] initWithFrame:CGRectMake(0, 135.0, SCREEN_WIDTH, 200.0)];
    self.lineChart.showCoordinateAxis = YES;
//    self.lineChart.yLabelFormat = @"%1.1f";
//    self.lineChart.xLabelFont = [UIFont fontWithName:@"Helvetica-Light" size:8.0];
    //        [self.lineChart setXLabels:@[@"SEP 1", @"SEP 2", @"SEP 3", @"SEP 4", @"SEP 5", @"SEP 6", @"SEP 7"]];
//    [self.lineChart setXLabels:@[@"1", @"2", @"3", @"4", @"5", @"6", @"7", @"8", @"9", @"10", @"11"]];
    self.lineChart.yLabelColor = [UIColor blackColor];
    self.lineChart.xLabelColor = [UIColor blackColor];
    self.lineChart.yUnit = @"¥";
    
    // added an example to show how yGridLines can be enabled
    // the color is set to clearColor so that the demo remains the same
    self.lineChart.showGenYLabels = YES;
    self.lineChart.showYGridLines = YES;
    
    //Use yFixedValueMax and yFixedValueMin to Fix the Max and Min Y Value
    //Only if you needed
    self.lineChart.yFixedValueMax = 20000;
    self.lineChart.yFixedValueMin = 0.0;
//
//    [self.lineChart setYLabels:@[
//                                 @"0",
//                                 @"50",
//                                 @"100",
//                                 @"150",
//                                 @"200",
//                                 @"250",
//                                 @"300",
//                                 ]
//     ];
    
//    // Line Chart #1
//    NSArray *data01Array = @[@124, @134, @128, @127, @144, @139, @134, @134, @132, @130, @116, @95];
//    PNLineChartData *data01 = [PNLineChartData new];
//
//    data01.rangeColors = @[
//                           [[PNLineChartColorRange alloc] initWithRange:NSMakeRange(1, 54) color:[UIColor redColor]],
//                           [[PNLineChartColorRange alloc] initWithRange:NSMakeRange(55, 6) color:[UIColor yellowColor]],
//                           [[PNLineChartColorRange alloc] initWithRange:NSMakeRange(61, 49) color:[UIColor greenColor]],
//                           [[PNLineChartColorRange alloc] initWithRange:NSMakeRange(110, 20) color:[UIColor yellowColor]],
//                           [[PNLineChartColorRange alloc] initWithRange:NSMakeRange(130, 470) color:[UIColor redColor]]
//                           ];
//    data01.dataTitle = @"Alpha";
//    data01.color = PNFreshGreen;
//    data01.pointLabelColor = [UIColor blackColor];
//    data01.alpha = 0.3f;
//    data01.showPointLabel = YES;
//    data01.pointLabelFont = [UIFont fontWithName:@"Helvetica-Light" size:9.0];
//    data01.itemCount = data01Array.count;
//    data01.inflexionPointColor = PNRed;
//    data01.inflexionPointStyle = PNLineChartPointStyleTriangle;
//    data01.getData = ^(NSUInteger index) {
//        CGFloat yValue = [data01Array[index] floatValue];
//        return [PNLineChartDataItem dataItemWithY:yValue];
//    };
//
//    // Line Chart #2
//    NSArray *data02Array = @[@0.0, @18.1, @26.4, @30.2, @12.2, @16.2, @27.2];
//    PNLineChartData *data02 = [PNLineChartData new];
//    data02.dataTitle = @"Beta";
//    data02.pointLabelColor = [UIColor blackColor];
//    data02.color = PNTwitterColor;
//    data02.alpha = 0.5f;
//    data02.itemCount = data02Array.count;
//    data02.inflexionPointStyle = PNLineChartPointStyleCircle;
//    data02.getData = ^(NSUInteger index) {
//        CGFloat yValue = [data02Array[index] floatValue];
//        return [PNLineChartDataItem dataItemWithY:yValue];
//    };
    
//    self.lineChart.chartData = @[data01, data02];
    
    
    
//    [self.lineChart strokeChart];
    self.lineChart.delegate = self;
    
    
    [self.view addSubview:self.lineChart];
    
    self.lineChart.legendStyle = PNLegendItemStyleStacked;
    self.lineChart.legendFont = [UIFont boldSystemFontOfSize:12.0f];
    self.lineChart.legendFontColor = [UIColor redColor];
    
    
   
    
    OrgReportActionInfo *orgReportActionInfo = [[OrgReportActionInfo alloc] init];
    orgReportActionInfo.startDate = [self getFirstDayOfThisYear];
    [HttpReportAction getOrgReportAction:orgReportActionInfo block:^(id result, NSError *error) {
        if (!error) {
            [self setLine:result];
        }
    }];
}


-(void)setLine:(NSArray*)array{
    NSMutableArray *monthArray = [NSMutableArray array];
    NSMutableArray *monthPorfitArray = [NSMutableArray array];
    OrgMainReportInfo *orgMainInfo = array[0];
    for (OrgReportInfo *orgReportInfo in orgMainInfo.monthProfitList) {
        [monthArray addObject:orgReportInfo.month];
        [monthPorfitArray addObject:[NSNumber numberWithDouble:orgReportInfo.profit]];
    }
    [self.lineChart setXLabels:monthArray];

    
    NSArray *data02Array = monthPorfitArray;
    PNLineChartData *data02 = [PNLineChartData new];
    data02.dataTitle = orgMainInfo.orgName;
    data02.pointLabelColor = [UIColor blackColor];
    data02.showPointLabel = YES;
    data02.pointLabelFont = [UIFont systemFontOfSize:9];
    data02.color = PNTwitterColor;
    data02.alpha = 0.5f;
    data02.itemCount = data02Array.count;
    data02.inflexionPointStyle = PNLineChartPointStyleCircle;
    data02.getData = ^(NSUInteger index) {
        CGFloat yValue = [data02Array[index] floatValue];
        return [PNLineChartDataItem dataItemWithY:yValue];
    };
    
    self.lineChart.chartData = @[data02];
    [self.lineChart.chartData enumerateObjectsUsingBlock:^(PNLineChartData *obj, NSUInteger idx, BOOL *stop) {
        obj.pointLabelColor = [UIColor blackColor];
    }];
    [self.lineChart strokeChart];

    
    UIView *legend = [self.lineChart getLegendWithMaxWidth:320];
    [legend setFrame:CGRectMake(30, 340, legend.frame.size.width, legend.frame.size.width)];
//    legend.backgroundColor = [UIColor blueColor];
    [self.view addSubview:legend];
}

-(NSString*)getFirstDayOfThisYear{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy"];
    NSString *currentDateStr = [dateFormatter stringFromDate:[NSDate date]];
    return [NSString stringWithFormat:@"%@-01-01",currentDateStr];
}


- (void)userClickedOnLinePoint:(CGPoint)point lineIndex:(NSInteger)lineIndex{
    
}

- (void)userClickedOnLineKeyPoint:(CGPoint)point
                        lineIndex:(NSInteger)lineIndex
                       pointIndex:(NSInteger)pointIndex{
    
}

- (void)userClickedOnBarAtIndex:(NSInteger)barIndex{
    
}


- (void)userClickedOnPieIndexItem:(NSInteger)pieIndex{
    
}
- (void)didUnselectPieItem{
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
